package model

import (
	"time"
)

// TicketStatus 工单状态
type TicketStatus string

const (
	TicketStatusNew        TicketStatus = "new"        // 新建
	TicketStatusOpen       TicketStatus = "open"       // 已打开
	TicketStatusPending    TicketStatus = "pending"    // 处理中
	TicketStatusResolved   TicketStatus = "resolved"   // 已解决
	TicketStatusClosed     TicketStatus = "closed"     // 已关闭
	TicketStatusReopened   TicketStatus = "reopened"   // 已重新打开
	TicketStatusCancelled  TicketStatus = "cancelled"  // 已取消
)

// TicketPriority 工单优先级
type TicketPriority string

const (
	TicketPriorityLow    TicketPriority = "low"    // 低优先级
	TicketPriorityMedium TicketPriority = "medium" // 中优先级
	TicketPriorityHigh   TicketPriority = "high"   // 高优先级
	TicketPriorityUrgent TicketPriority = "urgent" // 紧急
)

// TicketType 工单类型
type TicketType string

const (
	TicketTypeInquiry    TicketType = "inquiry"    // 咨询
	TicketTypeComplaint  TicketType = "complaint"  // 投诉
	TicketTypeRequest    TicketType = "request"    // 请求
	TicketTypeIncident   TicketType = "incident"   // 事件
	TicketTypeProblem    TicketType = "problem"    // 问题
	TicketTypeChange     TicketType = "change"     // 变更
)

// TicketSource 工单来源
type TicketSource string

const (
	TicketSourcePhone    TicketSource = "phone"    // 电话
	TicketSourceEmail    TicketSource = "email"    // 邮件
	TicketSourceChat     TicketSource = "chat"     // 在线聊天
	TicketSourceWeb      TicketSource = "web"      // 网站表单
	TicketSourceSocial   TicketSource = "social"   // 社交媒体
	TicketSourceInternal TicketSource = "internal" // 内部创建
)

// Ticket 工单
type Ticket struct {
	ID           string       `json:"id" bson:"_id"`
	Title        string       `json:"title" bson:"title"`
	Description  string       `json:"description" bson:"description"`
	Status       TicketStatus `json:"status" bson:"status"`
	Priority     TicketPriority `json:"priority" bson:"priority"`
	Type         TicketType   `json:"type" bson:"type"`
	Source       TicketSource `json:"source" bson:"source"`
	CustomerID   string       `json:"customer_id" bson:"customer_id"`
	CustomerName string       `json:"customer_name" bson:"customer_name"`
	CustomerPhone string      `json:"customer_phone" bson:"customer_phone"`
	CustomerEmail string      `json:"customer_email" bson:"customer_email"`
	AssignedToID string       `json:"assigned_to_id" bson:"assigned_to_id"`
	AssignedToName string     `json:"assigned_to_name" bson:"assigned_to_name"`
	DepartmentID string       `json:"department_id" bson:"department_id"`
	Tags         []string     `json:"tags" bson:"tags"`
	RelatedCallID string      `json:"related_call_id" bson:"related_call_id"`
	RelatedRecordings []string `json:"related_recordings" bson:"related_recordings"`
	DueDate      time.Time    `json:"due_date" bson:"due_date"`
	ResolutionTime time.Time  `json:"resolution_time" bson:"resolution_time"`
	Resolution   string       `json:"resolution" bson:"resolution"`
	CreatorID    string       `json:"creator_id" bson:"creator_id"`
	CreatorName  string       `json:"creator_name" bson:"creator_name"`
	Comments     []TicketComment `json:"comments" bson:"comments"`
	Attachments  []Attachment   `json:"attachments" bson:"attachments"`
	Metadata     map[string]interface{} `json:"metadata" bson:"metadata"`
	CreatedAt    time.Time     `json:"created_at" bson:"created_at"`
	UpdatedAt    time.Time     `json:"updated_at" bson:"updated_at"`
}

// TicketComment 工单评论
type TicketComment struct {
	ID         string    `json:"id" bson:"id"`
	Content    string    `json:"content" bson:"content"`
	AuthorID   string    `json:"author_id" bson:"author_id"`
	AuthorName string    `json:"author_name" bson:"author_name"`
	IsPrivate  bool      `json:"is_private" bson:"is_private"` // 是否为内部评论
	CreatedAt  time.Time `json:"created_at" bson:"created_at"`
	Attachments []Attachment `json:"attachments" bson:"attachments"`
}

// Attachment 附件
type Attachment struct {
	ID       string    `json:"id" bson:"id"`
	FileName string    `json:"file_name" bson:"file_name"`
	FileType string    `json:"file_type" bson:"file_type"`
	FileSize int64     `json:"file_size" bson:"file_size"`
	FilePath string    `json:"file_path" bson:"file_path"`
	CreatedAt time.Time `json:"created_at" bson:"created_at"`
}

// TicketHistory 工单历史记录
type TicketHistory struct {
	ID        string    `json:"id" bson:"_id"`
	TicketID  string    `json:"ticket_id" bson:"ticket_id"`
	FieldName string    `json:"field_name" bson:"field_name"`
	OldValue  string    `json:"old_value" bson:"old_value"`
	NewValue  string    `json:"new_value" bson:"new_value"`
	ChangedByID string  `json:"changed_by_id" bson:"changed_by_id"`
	ChangedByName string `json:"changed_by_name" bson:"changed_by_name"`
	CreatedAt time.Time `json:"created_at" bson:"created_at"`
}

// TicketAssignment 工单分配记录
type TicketAssignment struct {
	ID          string    `json:"id" bson:"_id"`
	TicketID    string    `json:"ticket_id" bson:"ticket_id"`
	AssignedToID string   `json:"assigned_to_id" bson:"assigned_to_id"`
	AssignedToName string `json:"assigned_to_name" bson:"assigned_to_name"`
	AssignedByID string   `json:"assigned_by_id" bson:"assigned_by_id"`
	AssignedByName string `json:"assigned_by_name" bson:"assigned_by_name"`
	AssignedAt  time.Time `json:"assigned_at" bson:"assigned_at"`
	Reason      string    `json:"reason" bson:"reason"`
}

// TicketSearchCriteria 工单搜索条件
type TicketSearchCriteria struct {
	Title        string       `json:"title" form:"title"`
	Status       string       `json:"status" form:"status"`
	Priority     string       `json:"priority" form:"priority"`
	Type         string       `json:"type" form:"type"`
	Source       string       `json:"source" form:"source"`
	CustomerID   string       `json:"customer_id" form:"customer_id"`
	CustomerName string       `json:"customer_name" form:"customer_name"`
	AssignedToID string       `json:"assigned_to_id" form:"assigned_to_id"`
	DepartmentID string       `json:"department_id" form:"department_id"`
	Tag          string       `json:"tag" form:"tag"`
	RelatedCallID string      `json:"related_call_id" form:"related_call_id"`
	StartDate    time.Time    `json:"start_date" form:"start_date"`
	EndDate      time.Time    `json:"end_date" form:"end_date"`
	CreatorID    string       `json:"creator_id" form:"creator_id"`
	DueDateFrom  time.Time    `json:"due_date_from" form:"due_date_from"`
	DueDateTo    time.Time    `json:"due_date_to" form:"due_date_to"`
	Page         int          `json:"page" form:"page"`
	PageSize     int          `json:"page_size" form:"page_size"`
	SortBy       string       `json:"sort_by" form:"sort_by"`
	SortOrder    string       `json:"sort_order" form:"sort_order"`
}

// TicketCreateRequest 创建工单请求
type TicketCreateRequest struct {
	Title           string       `json:"title" binding:"required"`
	Description     string       `json:"description" binding:"required"`
	Type            TicketType   `json:"type" binding:"required"`
	Source          TicketSource `json:"source" binding:"required"`
	Priority        TicketPriority `json:"priority"`
	CustomerID      string       `json:"customer_id"`
	CustomerName    string       `json:"customer_name" binding:"required_without=CustomerID"`
	CustomerPhone   string       `json:"customer_phone"`
	CustomerEmail   string       `json:"customer_email"`
	DepartmentID    string       `json:"department_id"`
	Tags            []string     `json:"tags"`
	RelatedCallID   string       `json:"related_call_id"`
	DueDate         time.Time    `json:"due_date"`
	AssignedToID    string       `json:"assigned_to_id"`
	Metadata        map[string]interface{} `json:"metadata"`
}

// TicketUpdateRequest 更新工单请求
type TicketUpdateRequest struct {
	Title           string       `json:"title"`
	Description     string       `json:"description"`
	Status          TicketStatus `json:"status"`
	Priority        TicketPriority `json:"priority"`
	Type            TicketType   `json:"type"`
	DepartmentID    string       `json:"department_id"`
	Tags            []string     `json:"tags"`
	DueDate         time.Time    `json:"due_date"`
	Resolution      string       `json:"resolution"`
	Metadata        map[string]interface{} `json:"metadata"`
}

// TicketAssignRequest 分配工单请求
type TicketAssignRequest struct {
	AssignedToID    string    `json:"assigned_to_id" binding:"required"`
	Reason          string    `json:"reason"`
}

// TicketCommentRequest 添加评论请求
type TicketCommentRequest struct {
	Content         string      `json:"content" binding:"required"`
	IsPrivate       bool        `json:"is_private"`
}

// TicketStatistics 工单统计
type TicketStatistics struct {
	TotalTickets    int64       `json:"total_tickets"`
	ByStatus        map[string]int64 `json:"by_status"`
	ByPriority      map[string]int64 `json:"by_priority"`
	ByType          map[string]int64 `json:"by_type"`
	ByDepartment    map[string]int64 `json:"by_department"`
	AverageResolutionTime float64 `json:"average_resolution_time"`
	OpenTickets     int64       `json:"open_tickets"`
	OverdueTickets  int64       `json:"overdue_tickets"`
	TodayTickets    int64       `json:"today_tickets"`
}